package org.txazo.music.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.txazo.music.dao.FileDao;

/**
 * 文件数据访问实现
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 */
@Repository("fileDao")
public class FileDaoImpl implements FileDao {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
		return namedParameterJdbcTemplate;
	}

	public void setNamedParameterJdbcTemplate(
			NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
		this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
	}

	@Override
	public boolean isMusicFileExist(String hash) {
		return isFileExist(hash, "mp3");
	}

	@Override
	public boolean isFileExist(String hash, String type) {
		String sql = "select count(1) from file where hash = ? and type = ?";
		return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(
				sql, Integer.class, new Object[] { hash, type }) == 1;
	}

}
